package com.cogent.report.display.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.common.util.MapUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.type.DistrictControl;
import com.cogent.report.display.IReportDisplay;
import com.cogent.report.domain.ReportDisplayFieldDomain;

public class LevelSelectDisplayImpl implements IReportDisplay {	
	public String getDisplayValue(ReportDisplayFieldDomain displayFieldDomain, HashMap dataMap) {
		String[] typecodes = displayFieldDomain.getTypecode().split("\\|");
		String sql = typecodes[0];
		String orderby = "";
		if(typecodes.length > 1)
			orderby = typecodes[1];
		List queryList = new ArrayList();
		HashMap curQueryMap = new HashMap();
		if(typecodes.length > 2) {
			String[] param = typecodes[2].split(";");
			for(int i=0; i<param.length; i++) {
				String[] curtemps = param[i].split("~");
				String value = MapUtil.getStringValue(dataMap, curtemps[0]);
				if(curtemps[0].equals("districtcode"))
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], DistrictControl.getControlCode(value), DistrictControl.getQueryType(value)));
				else
					queryList.add(new QueryParamDomain(curtemps[1], curtemps[0], value));
				curQueryMap.put(curtemps[0], value);
			}
		}
		String defaultValue = MapUtil.getStringValue(dataMap, displayFieldDomain.getName());
		int keyindex = 0;
		if(typecodes.length > 3) {
			String[] curtemp = typecodes[3].split("~");
			for(int i=0; i<curtemp.length; i++) {
				if(curtemp[i].equals(displayFieldDomain.getName()))
					keyindex = i;
			}
		}
		List list = BussinessQueryHelper.query(sql, queryList, orderby); 
		String ret = "";
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap curdataMap = (HashMap)it.next();
			String[] enumvalue = MapUtil.getStringValue(curdataMap, "enumvalue").split("~");
			if(enumvalue[keyindex].equals(defaultValue)) {
				ret = MapUtil.getStringValue(curdataMap, "enumoption");
				break;
			}
		}
		
		return ret;
	}
}
